#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    SetGameMode();
    connect(ui->Btn_ExitGame,SIGNAL(clicked()),this,SLOT(Btn_ExitGame_Click()));
    connect(ui->Btn_StartGame,SIGNAL(clicked()),this,SLOT(Btn_StartGame_Click()));
    ui->StatusBar->showMessage(GameModeTranslate(),0);

}

MainWindow::~MainWindow()
{
    delete ui;
}

QString MainWindow::GameModeTranslate() {
    if (gameMode == LocalNon) {
        return "当前游戏模式：本地无禁手";
    }
    else if (gameMode == Local) {
        return "当前游戏模式：本地有禁手";
    }
    else if (gameMode == NetworkNon) {
        return "当前游戏模式：网络无禁手";
    }
    else if (gameMode == Network) {
        return "当前游戏模式：网络有禁手";
    }
    else {
        return "当前游戏模式：人机无禁手";
    }
}

void MainWindow::Btn_ExitGame_Click() {
    Action_Exit();
}

void MainWindow::Btn_StartGame_Click() {
    if (gameMode == Network || gameMode ==  NetworkNon) {
        this->hide();
        connect(&b,SIGNAL(sendsignal()),this,SLOT(ReShow()));
        b.initialization();
        b.show();
    }
    else if (gameMode == AI) {
        QMessageBox::question(NULL,"提示","人机对弈模式暂未开放，敬请期待");
        return;
    }
    else {
        this->hide();
        connect(&b,SIGNAL(sendsignal()),this,SLOT(ReShow()));
        b.initialization();
        b.show();
    }
    qDebug() << "game start";
}

void MainWindow::SetGameMode(GameMode mode) {
    gameMode = mode;
    ui->StatusBar->showMessage(GameModeTranslate(),0);
    qDebug() << mode;
}

void MainWindow::Action_Exit() {
    int ret = QMessageBox::question(NULL,"警告","要退出游戏吗？");
    if (ret == QMessageBox::Yes) {
        close();
        qDebug() << "game close";
    }
    else {
        qDebug() << "close cancel";
    }
}

void MainWindow::on_Action_LocalNon_triggered() {
    SetGameMode(LocalNon);
    b.gameMode = gameMode;
}

void MainWindow::on_Action_Local_triggered() {
    SetGameMode(Local);
    b.gameMode = gameMode;
}

void MainWindow::on_Action_NetworkNon_triggered() {
    SetGameMode(NetworkNon);
    b.gameMode = gameMode;
}

void MainWindow::on_Action_Network_triggered() {
    SetGameMode(Network);
    b.gameMode = gameMode;
}

void MainWindow::on_Action_AI_triggered() {
    SetGameMode(AI);
    b.gameMode = gameMode;
}

void MainWindow::on_Action_NetworkSettings_triggered() {
    n.exec();
    b.IP = n.IPAddr;
    b.port = n.Port;
    b.username = n.YourName;
    b.isServer = n.isServer;
    // qDebug() << QString::fromStdString(b.username);
}

void MainWindow::on_Action_Info_triggered() {
    a.exec();
}

void MainWindow::on_Action_Settings_triggered() {
    s.exec();
    b.steptime = s.StepTime;
    b.gametime = s.GameTime;
}

void MainWindow::ReShow() {
    this->show();
    SetGameMode(b.gameMode);
}

void MainWindow::ReTurn() {
    // b.connected = c.connected;
    // b.connected = false;
}
